<template>
  <div class="goodsinfo">
    <transition @before-enter="beforeenter" @enter="enter" @after-enter="afterenter">
      <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>

    <!-- 商品轮播 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :showimagelist="lunbotu"></swiper>
        </div>
      </div>
    </div>

    <!-- 商品购买 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfolist.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{goodsinfolist.market_price}}</del>&nbsp;&nbsp;销售价
            <span class="nowprice">￥{{goodsinfolist.sell_price}}</span>
          </p>
          <p>
            购买数量：
            <numbox @getcount="getselectedCount" :max="goodsinfolist.stock_quantity"></numbox>
          </p>
          <P>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addcart">加入购物车</mt-button>
          </P>
        </div>
      </div>
    </div>

    <!-- 商品参数 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsinfolist.goods_no}}</p>
          <p>库存型号：{{goodsinfolist.stock_quantity}}件</p>
          <p>上架时间：{{goodsinfolist.add_time|dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="gopicture(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goword(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../soncomponent/swiper.vue";
// 导入数字选择框
import numbox from "../soncomponent/goosinfonumbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotu: [],
      goodsinfolist: {},
      ballflag: false, //控制小球标识符
      selectedCount: 1 //保存用户选中的商品数量，默认用户买一件
    };
  },
  methods: {
    getlunbo() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status == 0) {
          //循环轮播图数组每一项
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbotu = result.body.message;
        }
      });
    },
    getgoodsinfolist() {
      //获取商品信息
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status == 0) {
          this.goodsinfolist = result.body.message[0];
        }
      });
    },
    gopicture(id) {
      //去图文介绍
      this.$router.push({ path: "/index/goodspicture/" + this.id });
    },
    goword(id) {
      //去商品品论

      this.$router.push({ path: "/index/goodsword/" + this.id });
    },
    addcart() {
      this.ballflag = !this.ballflag;

      // 拼接的一个保存到store中cart数组中的商品对象
      var goodslist = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfolist.sell_price,
        selected: true
      };
      //调用store中的mutations的方法
      this.$store.commit('addTocart',goodslist)
    },
    beforeenter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;

      // 获取小球在页面中的位置
      const ballposition = this.$refs.ball.getBoundingClientRect();
      //  获取徽标在页面中的位置
      const badgeposition = document
        .getElementById("badge")
        .getBoundingClientRect();

      var x = badgeposition.left - ballposition.left;
      var y = badgeposition.top - ballposition.top;
      el.style.transform = `translate(${x}px,${y}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterenter(el) {
      this.ballflag = !this.ballflag;
    },
    getselectedCount(count) {
      this.selectedCount = count;
      console.log(this.selectedCount);
    }
  },
  created() {
    this.getlunbo();
    this.getgoodsinfolist();
  },
  components: {
    swiper,
    numbox
  },
  props: ["showimagelist"]
};
</script>
<style lang="scss">
.goodsinfo {
  background-color: #eee;
  overflow: hidden;
  .nowprice {
    color: red;
    font-size: 18px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 16px 0;
    }
  }
  .ball {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 20;
    top: 505px;
    left: 150px;
  }
}
</style>